** QCEW number of establishments by industry 
** JHL
	
*************************************
** Set up workspace
*************************************
version 14.0
clear all
set more off

cd "${path_home}"
adopath + ../../programs 

** log using "${path_log}/c03_qcew_county_DFM", text replace

*************************************
** Start work here
*************************************
timer on 1 

*************************************
** [1] Keep Nielsen industries in QCEW data, create channel codes 
*************************************

foreach i of numlist 2004/2015 {
	use "${path_big_dta}/qcew/`i'_q_singlefile", clear
	* 4-digit NAICS, keep relevant industries 
	keep if industry_code=="4451"|industry_code=="44511"|industry_code=="44512"|industry_code=="4461"|industry_code=="4521"|industry_code=="4529"|industry_code=="722"|industry_code=="7225"|industry_code=="722511"|industry_code=="722513"
	* Private 
	keep if own_code==5
	keep area_fips industry_code year qtr disclosure_code qtrly_estabs avg_wkly_wage lq_disclosure_code lq_qtrly_estabs month1_emplvl month2_emplvl month3_emplvl
	save "${path_big_dta}/qcew/qcew_444572_`i'", replace
}

clear 
foreach i of numlist 2004/2015 {
	append using "${path_big_dta}/qcew/qcew_444572_`i'"
}

drop if area_fips==.

gen fips_state_code=int(area_fips/1000)
gen fips_county_code=area_fips-fips_state_code*1000

rename qtr quarter

save "${path_big_dta}/qcew/qcew_444572_0415", replace

gen channel_code="D" if industry_code=="4461"
replace channel_code="F" if industry_code=="4451"
replace channel_code="M" if industry_code=="4521" 

drop if channel_code=="" 

save "${path_big_dta}/qcew/qcew_DFM_0415", replace

*************************************
** Close workspace
*************************************
timer off 1
timer list 1
** log close

